<?php
if (preg_match ( "/".basename ( __FILE__ )."/", $_SERVER ['PHP_SELF'] )) {
	die ("<h1>Incorrect access</h1>You cannot access this file directly.");
}

class index {
	function playme(){
		$code = FunctionLib::getParam('code');
		switch( $code )
		{
			case 'unbelive':
				$this->unbelive();
				break;	
			case 'belive':
				$this->belive();
				break;	
			case 'shareBeliveToWall':
				$this->shareBeliveToWall();
				break;	
			default:
				$this->home();
				break;
		}
	}
	
	function unbelive() {
		global $facebook;
		$fbme = $facebook->api('/me'); //prt($fbme);
		
		$id = $fbme['id']; // id user unbelive
		$pid = FunctionLib::getParamInt('pid', 0); // post id
		if($id > 0  && $pid > 0) {
			$post = DB::fetch("SELECT * FROM ". T_POST_BELIVE. " WHERE id = $pid LIMIT 0,1");
			if($post) {
				$belivers = unserialize($post['belivers']);
				if(isset($belivers[$id])) {
					unset($belivers[$id]);
					$valUpdate = array(
						'num_belive' => $post['num_belive'] - 1,
						'belivers' => serialize($belivers),
						'updated' => TIME_NOW
					);
					
					DB::update(T_POST_BELIVE, $valUpdate, "id = $pid");
					DB::delete(T_BELIVE, "fbid = $id AND post_id = $pid");
					
					$data = array(
						'fbid' => $fbme['id'],
						'fbname' => $fbme['name']
					);
					echo FunctionLib::JsonSuccess('success', array('data' => $data));exit();
				}
				else{
					echo FunctionLib::JsonErr('Lỗi');exit();
				}
			}
			else {
				echo FunctionLib::JsonErr('Không có post');exit();
			}
		}
		else {
			echo FunctionLib::JsonErr('Không hợp lệ');exit();
		}
	}
	
	function belive() {
		global $facebook;
		$fbme = $facebook->api('/me'); //prt($fbme);
		
		$id = $fbme['id']; // id user unbelive
		$pid = FunctionLib::getParamInt('pid', 0); // post id
		if($id > 0  && $pid > 0) {
			
			$post = DB::fetch("SELECT * FROM ". T_POST_BELIVE. " WHERE id = $pid LIMIT 0,1");
			if($post) {
				$belivers = unserialize($post['belivers']);
				
				if(!isset($belivers[$id])) {
					$belivers[$id] = $fbme['name'];
					$valUpdate = array(
						'num_belive' => $post['num_belive'] + 1,
						'belivers' => serialize($belivers),
						'updated' => TIME_NOW
					);
					DB::update(T_POST_BELIVE, $valUpdate, "id = $pid");
					DB::insert(T_BELIVE, array('fbid' => $id, 'post_id' => $pid, 'created' => TIME_NOW));
					
					$properties = array();
					$properties['Facebook'] = array('text' => $post['fbname'], 'http://facebook.com/profile.php?id='. $post['fbid']);
					$data = array (
						'message'       => 'tin rằng:',
						'description'	=> PostLib::getContentBelive(array('owner' => $post['owner'], 'fbid' => $post['fbid'], 'fbname' => $post['name'], 'content' => $post['content'])),
						'text'			=> ($input['type']) ? 'Tin '.$post['fbname'] : 'Tin điều này',
						'title'			=> ($input['type']) ? $post['fbname'] : '',
						'src'			=> ($input['type']) ? FaceLib::getProfileImageUrl($post['fbid']) : '',
						'link'			=> FB_BASE_URL.'profile.php?id='. $post['fbid'],
						'link_image'	=> FB_BASE_URL.'profile.php?id='. $post['fbid'],
						'properties'	=> $properties
					);
					FaceLib::postContentToWall($data);
					
					$data = array(
						'fbid' => $fbme['id'],
						'fbname' => $fbme['name']
					);
					echo FunctionLib::JsonSuccess('success', array('data' => $data));exit();
				}
				else{
					echo FunctionLib::JsonErr('Lỗi');exit();
				}
			}
			else {
				echo FunctionLib::JsonErr('Không có post');exit();
			}
		}
		else {
			echo FunctionLib::JsonErr('Không hợp lệ');exit();
		}
	}
	
	function shareBeliveToWall() {
		global $facebook;
		$id = FunctionLib::getParamInt('id', 0); // id user unbelive
		$pid = FunctionLib::getParamInt('pid', 0); // post id
		
		$post = PostLib::getPost($pid);
		if($post) {
			$user = $facebook->getUser();
			if($user) {
				$data = array (
					'message'       => PostLib::getOtherBeliveForPost($post, $id, $post['fbid']),
					'description'	=> PostLib::getContentBelive(array('owner' => $post['owner'], 'fbid' => $post['fbid'], 'fbname' => $post['fbname'], 'content' => $post['content'])),
					'text'			=> ($post['owner']) ? 'Tin '.$post['fbname'] : 'Tin điều này',
					'title'			=> ($post['owner']) ? $post['fbname'] : '',
					'src'			=> ($post['owner']) ? FaceLib::getProfileImageUrl($post['fbid']) : '',
					'link'			=> FB_BASE_URL.'profile.php?id='. $post['fbid'],
					'link_image'	=> FB_BASE_URL.'profile.php?id='. $post['fbid'],
					'caption'		=> ''
				);
				//post to Facebook
				FaceLib::postContentToWall($data);
				
				echo FunctionLib::JsonSuccess('success');exit();
			}
			else{
				echo FunctionLib::JsonErr('Chưa kết nối facebook');exit();
			}
		}
		else {
			echo FunctionLib::JsonErr('Không có post');exit();
		}
	}

	function home(){
		global $display;
		die("Nothing to do...");
	}
}
?>